
var repairTel;
var articleinfo;
var datalist = [];
var advList=[];  //广告数据

apiready = function() {
	
    api.setRefreshHeaderInfo({
		visible: true,
		bgColor: '#f2f2f2',
        textColor: '#4d4d4d',
	    textDown: '下拉刷新...',
	    textUp: '松开刷新...',
	    showTime: true
    },function(ret,err){
	    loadData();
	    api.refreshHeaderLoadDone();
    });
    
    api.addEventListener({
	    name: 'refreshProperty'
	}, function(ret){
	    if(ret){
	      loadData();
	    }
	});
		
    loadData();
}

//设置滑动图片
function openScrollPicture() {
    
    var paths = ['/image/defaultAdv.jpg'];
    
    if(advList && advList.length > 0){
    	paths = [];
    	for(var i = 0; i < advList.length; i++) {
			paths.push(serviceUrl + advList[i].img);
	   	}
    }

	var obj = api.require('UIScrollPicture');	
	
	obj.open({
	    rect: {
	        x: 0, y: 0, w:api.winWidth, h:200
	    },
	    data: {
	        paths: paths,
	    },
	    styles: {
	        indicator: {
	            align: 'right',
	            color: '#666',
	            activeColor: '#ffaa43'
	        }
	    },
	    placeholderImg: 'widget://image/defaultAdv.jpg',
	    contentMode: 'scaleToFill',
	    interval:5,
	    loop: true,
	    fixedOn: 'property',
	    fixed: false
		}, function(ret, err){
			if(advList.length > 0){
		        if(ret.eventType == 'click'){
		            //点击图片的操作
		            openAdvert(ret.index);
				}
			}
		}
	);
	
	obj.reloadData({
	    data: {
	        paths: paths,
	    }
	});
}

//加载数据
function loadData() {
	advList = [];
	
	var account = $api.getStorage('account');
	if (!account) {
		$api.byId("tip").innerHTML = "提示";
       	$api.byId('noticetitle').innerHTML = "亲，您还没登录，请先登录";
       	openScrollPicture();
      	return;
	}

	var member2 = $api.getStorage('member2');
	if (member2&&member2.status == '未认证') {
		$api.byId("tip").innerHTML = "提示";
     	$api.byId('noticetitle').innerHTML = "亲，您还未认证小区哦，去认证";
     	openScrollPicture();
     	return;
	}

	api.showProgress({
        title: '加载中...',
        modal: false
    });
    var getUrl = serviceUrl + "/app/myproperty?account=" + account;
	api.ajax({
        url: getUrl,
        method: 'get',
        cache: false,
        timeout: 20,
        headers: {
            "Content-type": "application/json;charset=UTF-8"
        }
    }, function (ret, err) {
    	 if (ret.datas) {
    	 	
    	 	$api.byId("tip").innerHTML = "最新公告";
    	 	$api.byId('noticetitle').innerHTML = ret.datas.title||'';
            articleinfo=ret.datas
            repairTel=ret.repairTel;  //物业电话
            
            advList = ret.adverts;   //广告
        	
        } else {
          	$api.byId("tip").innerHTML = "提示";
            api.alert({msg: err.msg});
        }
        
        openScrollPicture();
        api.hideProgress();
    });
}


//是否登陆
function checkLogin() {
	if (!$api.getStorage('account')) {
		api.openWin({
	        name: 'login',
	        url: '../shop/login.html'
	    });
	    return false;
	}
	return true;
}


//打开最新公告
function openArticleinfo(){
     if (!checkLogin()){
          api.openWin({
	        name: 'login',
	        url: '../shop/login.html'
	      });
	    
	    return;
     }
     
      if (!checkStatus()){
          api.openWin({
	        name: 'useredit',
	        url: 'useredit.html'
	    });
	    
	    return;
     }
     
     openWin('articleinfo',{article: articleinfo});
     

};


//拨打物业电话
function callTel(){

    if (!checkLogin()) return;
	if (!checkStatus()) return;
    
    api.call({type: 'tel_prompt',number: repairTel});

};



//是否认证状态
function checkStatus() {
	if ($api.getStorage('member2').status == '未认证') {
		api.toast({
	        msg:'亲，您还未通过小区身份认证哦',
	        location:'middle'
        });
        return false;
	}
	return true;
}


//公共报修
function openRepair() {

	if (!checkLogin()) return;
	if (!checkStatus()) return;
	
	api.openWin({
        name: 'repair',
        url: 'repair.html'
    });
}

//投诉建议
function openAdvise() {

	if (!checkLogin()) return;
	if (!checkStatus()) return;
	
	api.openWin({
        name: 'advise',
        url: 'advise.html'
    });
}



//物业缴费
function openFee() {

	if (!checkLogin()) return;
	if (!checkStatus()) return;
	
	api.openWin({
	    name: 'fee',
	    url: 'property_fee.html',
	    vScrollBarEnabled: false
    });
}

//物业账单
function openBill() {

	if (!checkLogin()) return;
	if (!checkStatus()) return;
	
	api.openWin({
	    name: 'bill',
	    url: 'property_bill.html'
    });
}

//物业公告
function openAnnounce() {

	if (!checkLogin()) return;
	if (!checkStatus()) return;

	var compounds = $api.getStorage('member2').compounds;
	api.openWin({
	    name: 'articlelist',
	    url: 'articlelist.html',
	    pageParam: {compounds: compounds, type: "物业公告"}
    });
}


//维修通知
function openNotice() {

	if (!checkLogin()) return;
	if (!checkStatus()) return;
	
	var compounds = $api.getStorage('member2').compounds;
	api.openWin({
	    name: 'articlelist',
	    url: 'articlelist.html',
	    pageParam: {compounds: compounds, type: "维修通知"}
    });
}

//业务指引
function openGuide() {
	
	if (!checkLogin()) return;
	if (!checkStatus()) return;
	
	var compounds = $api.getStorage('member2').compounds;
	api.openWin({
	    name: 'articlelist',
	    url: 'articlelist.html',
	    pageParam: {compounds: compounds, type: "业务指引"}
    });
}

//点击广告
function openAdvert(index) {
	
	if(advList && advList.length > 0){
		var advert = advList[index];
	
		openWin('property_advDetail', {'title':advert.name, 'content':advert.content});
	}
}
